package com.xja.controller;

import com.xja.entity.Bookinfo;
import com.xja.entity.Orderinfo;
import com.xja.entity.PageResult;


import com.xja.feignclient.OrderService;
import com.xja.service.BookinfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 周广瑞
 * @date 2022/5/4 17:06
 */
@RestController
@RequestMapping("book")
public class BookController {
    private Logger logger= LoggerFactory.getLogger(BookController.class);
    @Autowired
    private BookinfoService bookinfoService;

    @Autowired
    private OrderService orderService;

    //调用了图书业务中查询所有图书的功能
    @RequestMapping("find")
    public PageResult<List<Bookinfo>> findall(){
        logger.info("调用了图书业务查询所有的图书功能");
        List<Bookinfo> bookinfoList = bookinfoService.slectBook();
        return PageResult.success("成功",bookinfoList);
    }
    //调用了图书业务中查询所有图书的功能
    @RequestMapping("findall")
    public PageResult<List<Bookinfo>> findallweb(){
        logger.info("调用了图书业务中查询所有图书的功能");
        List<Bookinfo> bookinfoList =bookinfoService.slectBook();
        for(Bookinfo bookinfo1:bookinfoList){
            System.out.println("bookinfo1 = " + bookinfo1);
        }
        return PageResult.success("成功",bookinfoList);
    }

//按照图书id显示图书订单信息以及图书信息
    @RequestMapping("findbid")
    public PageResult<Map<String,Object>>FindBid(@RequestParam("bid") Integer bid){
        logger.info("显示图书订单信息以及图书信息");

        Bookinfo book = bookinfoService.selectByPrimaryKey(bid);
        if (book ==null)
            return PageResult.nullresult("图书不存在",null);

        PageResult<List<Orderinfo>> pageResult = orderService.FinBid(book.getBid());

        List<Orderinfo> orderinfoList = pageResult.getObj();
        Map<String,Object>userMap = new HashMap<>();
        userMap.put("book",book);
        userMap.put("orderinfoList",orderinfoList);

        return PageResult.success("成功",userMap);
    }

    //按照用户id 查询用户信息
    @RequestMapping("findbookbybid")
    public PageResult<Bookinfo>findBookByBid(@RequestParam("bid")Integer bid){
        logger.info("调用了订单业务查询所有的信息");
        Bookinfo book = bookinfoService.selectByPrimaryKey(bid);
        return PageResult.success("成功",book);

    }

    //添加图书
    @PostMapping("add")
    public PageResult<Map<String,Object>> add(Bookinfo bookinfo){
        logger.info("添加图书功能");
//        int random=(int)(Math.random()*200)>1000?1:0;
        bookinfo.setBid((int)(Math.random()*8999));

        bookinfoService.insert(bookinfo);
        return PageResult.success("成功",null);
    }
}
